<template>
	<view class="mine">
		<view class="top_op_box">
			<view :class="['op_item', item.haveMsg ? 'red_point' : '']" v-for="item in top_menus" :key="item.name"
				@click="goLink(item)">
				<template v-if="item.name != '分享'">
					<image class="icon-44" :src="item.icon" mode=""></image>
					<view class="op_item_name">{{ item.name }}</view>
				</template>
				<button class="share_btn" open-type="share" v-else>
					<image class="icon-44" src="../../static/images/icon/icon_share_btn.png" mode=""></image>
					<view class="op_item_name">分享</view>
				</button>
			</view>
		</view>
		<view class="user_box">
			<view class="user_img" @tap="goLink({ url: '/pages/mine/userInfo' })">
				<image :src="userInfo.member.image" mode="" v-if="userInfo.member.image"></image>
				<image src="../../static/images/icon/icon_defalut_img.png" mode="" v-else></image>
			</view>
			<view class="user_info">
				<view class="fn-sz-36 fn-cl-333333 mar-bottom-8" v-if="userInfo.member.real_name">
					{{ userInfo.member.real_name }}
				</view>
				<view class="fn-sz-36 fn-cl-333333 mar-bottom-8" v-else>微信用户</view>
				<view class="user_info_resources">
					<view class="resources_item" v-for="item in resourcesList" :key="item.name" @tap="goLink(item)">
						<view class="resources_type_name">{{ item.name }}:</view>
						<view class="resources_num">{{ item.num }}</view>
						<image class="icon-24" src="../../static/images/icon/icon_moer_btn.png" mode=""></image>
					</view>
				</view>
			</view>
		</view>
		<view class="user_op_menu">
			<view class="user_op_menu_item" v-for="item in userOpMenu" :key="item.name" @tap="goLink(item)">
				<image class="icon-32" :src="item.icon" mode=""></image>
				<span class="mar-left-8 fn-sz-26 fn-cl-333333">{{ item.name }}</span>
			</view>
		</view>
		<view class="area_order_box">
			<view class="area-title">生鲜订单</view>
			<view class="area_menu_list">
				<view class="order_menu_item" v-for="item in goodsOrderMenu" :key="item.name" @tap="goGoodsOrder(item)">
					<image class="icon-48" :src="item.icon" mode=""></image>
					<view class="fn-sz-22 fn-cl-333333 mar-top-12">{{ item.name }}</view>
					<view class="order_item_num" v-if="item.num != 0">{{ item.num }}</view>
				</view>
			</view>
		</view>
		<view class="area_order_box">
			<view class="area-title">一日游订单</view>
			<view class="area_menu_list">
				<view class="order_menu_item" v-for="item in travelOrderMenu" :key="item.name" @tap="goTourOrder(item)">
					<image class="icon-48" :src="item.icon" mode=""></image>
					<view class="fn-sz-22 fn-cl-333333 mar-top-12">{{ item.name }}</view>
					<view class="order_item_num" v-if="item.num != 0">{{ item.num }}</view>
				</view>
			</view>
		</view>
		<view class="area_order_box">
			<view class="area-title">商务合作</view>
			<view class="area_menu_list" id="gouper_menu">
				<view class="order_menu_item" v-for="item in businessMenu" :key="item.name" @tap="grouperFunc(item)">
					<image class="icon-48" :src="item.icon" mode=""></image>
					<view class="fn-sz-22 fn-cl-333333 mar-top-12">{{ item.name }}</view>
				</view>
			</view>
		</view>

		<view class="docement_box">
			<view class="fn-sz-28 mar-right-20" @tap="goLink({ url: '/pages/mine/userAgreement' })">《用户协议》</view>
			<view class="fn-sz-28 mar-right-20" @tap="goLink({ url: '/pages/mine/privacyPolicy' })">《隐私政策》</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				top_menus: [{
						name: '设置',
						icon: require('../../static/images/icon/icon_setting.png'),
						url: '/pages/mine/setingMenu'
					},
					{
						name: '消息',
						icon: require('../../static/images/icon/icon_message.png'),
						url: '/pages/mine/systemMessage'
					},
					{
						name: '分享',
						icon: require('../../static/images/icon/icon_share_btn.png')
					},
					{
						name: '签到',
						icon: require('../../static/images/icon/icon_sign_in_black.png'),
						url: '/pages/mine/signIn'
					}
				],
				resourcesList: [{
						name: '积分',
						num: 0,
						url: '/pages/mine/mineIntegral'
					},
					{
						name: '优惠券',
						num: 0,
						url: '/pages/coupon/coupon'
					},
					{
						name: '省钱卡',
						num: 0,
						url: '/pages/mine/saveMoney'
					},
					{
						name: '金币',
						num: 0,
						url: '/pages/mine/coinPage'
					}
				],
				userOpMenu: [{
						name: '客服',
						icon: require('../../static/images/icon/icon_customer_service.png')
					},
					{
						name: '发票',
						icon: require('../../static/images/icon/icon_invoice.png'),
						url: '/pages/invoice/invoice'
					},
					{
						name: '中奖记录',
						icon: require('../../static/images/icon/icon_zhongjiang.png'),
						url: '/pages/prizeRecords/prizeRecords'
					},
					{
						name: '认养记录',
						icon: require('../../static/images/icon/icon_renyang.png'),
						url: '/pages/adoptRecords/adoptRecords'
					},
					{
						name: '收货地址',
						icon: require('../../static/images/icon/icon_gps1.png'),
						url: '/pages/integralShop/intergralAddress'
					}
				],
				goodsOrderMenu: [{
						name: '待付款',
						icon: require('../../static/images/icon/icon_wait_pay.png'),
						status: 'unpay',
						type: 'unpay_num',
						num: 0
					},
					{
						name: '配送中',
						icon: require('../../static/images/icon/icon_wait_get_goods.png'),
						status: 'deliver',
						type: 'deliver_num',
						num: 0
					},
					{
						name: '待评价',
						icon: require('../../static/images/icon/icon_await_eva.png'),
						status: 'wait_comment',
						type: 'wait_comment_num',
						num: 0
					},
					{
						name: '售后',
						icon: require('../../static/images/icon/icon_return.png'),
						haveMsg: true,
						status: 'sale_service',
						type: 'sale_service_num',
						num: 0
					},
					{
						name: '全部订单',
						icon: require('../../static/images/icon/icon_all_order.png'),
						status: 'all',
						type: 'total_num',
						num: 0
					}
				],
				travelOrderMenu: [{
						name: '待付款',
						icon: require('../../static/images/icon/icon_wait_pay.png'),
						status: 'unpay',
						type: 'unpay_num',
						num: 0
					},
					{
						name: '待评价',
						icon: require('../../static/images/icon/icon_await_eva.png'),
						status: 'wait_comment',
						type: 'wait_comment_num',
						num: 0
					},
					{
						name: '已完成',
						icon: require('../../static/images/icon/icon_wait_get_goods.png'),
						status: 'done',
						type: 'done_num',
						num: 0
					},
					{
						name: '已取消',
						icon: require('../../static/images/icon/icon_return.png'),
						haveMsg: false,
						status: 'cancel',
						type: 'cancel_num',
						num: 0
					},
					{
						name: '全部订单',
						icon: require('../../static/images/icon/icon_all_order.png'),
						status: 'all',
						type: 'total_num',
						num: 0
					}
				],
				businessMenu: [{
						name: '团长入驻',
						icon: require('../../static/images/icon/icon_grouper_in.png'),
						url: '/pages/grouper/grouperEnter'
					},
					{
						name: '团长管理',
						icon: require('../../static/images/icon/icon_grouper_manage.png'),
						url: '/pages/grouper/grouperManage'
					}
				],
				userInfo: {}
			};
		},
		onShow() {
			this.getMemberInfo();
			this.getOrderNum();
		},
		onPullDownRefresh() {
			this.getMemberInfo();
			this.getOrderNum();
		},
		methods: {
			goLink({
				url,
				name
			}) {
				if (url) {
					uni.navigateTo({
						url
					});
				} else {
					switch (name) {
						case '客服':
							this.$api.getServiceTel().then(res => {
								if (res.code == 1) {
									uni.makePhoneCall({
										phoneNumber: res.data.tel //仅为示例
									});
								}
							});
							break;
					}
				}
			},
			grouperFunc(item) {
				this.$api.getTeamInfo().then(res => {
					console.log(res, '获取团长信息');
					if (!res.data.info.id) {
						uni.navigateTo({
							url: item.url
						});
					} else {
						switch (item.name) {
							case '团长入驻':
								if (res.data.info.check_status == 'wait') {
									this.$helper.toast('正在审核中');
								}
								if (res.data.info.check_status == 'accept') {
									this.$helper.toast('审核已通过');
								}
								if (res.data.info.check_status == 'refuse') {
									this.$helper.toast('审核已拒绝');
									uni.navigateTo({
										url: item.url
									});
								}
								break;
							case '团长管理':
								if (res.data.info.check_status == 'wait') {
									this.$helper.toast('正在审核中');
								}
								if (res.data.info.check_status == 'accept') {
									uni.navigateTo({
										url: `${item.url}?balance=${res.data.info.balance}`
									});
								}
								if (res.data.info.check_status == 'refuse') {
									this.$helper.toast('审核已拒绝');
								}
								break;
						}
					}
				});
			},
			goGoodsOrder(item) {
				uni.navigateTo({
					url: `/pages/order/goodsOrder?status=${item.status}`
				});
			},
			goTourOrder(item) {
				uni.navigateTo({
					url: `/pages/order/tourOrder?status=${item.status}`
				});
			},
			getMemberInfo() {
				this.$api.getMemberInfo().then(res => {
					console.log(res, '获取用户信息');
					if (res.code == 1) {
						this.userInfo = res.data;
						this.resourcesList[0].num = this.userInfo.member.score;
						this.resourcesList[1].num = this.userInfo.coupon_num;
						this.resourcesList[2].num = this.userInfo.member.balance;
						this.resourcesList[3].num = this.userInfo.member.coin;
						this.userInfo.sign == 'yes' ? (this.top_menus[3].haveMsg = false) : (this.top_menus[3]
							.haveMsg = true);
						uni.setStorageSync('userInfo', res.data);
					}
				});
			},
			getOrderNum() {
				this.$api.getTourOrderNum().then(res => {
					console.log(res, '获取订单数量');
					this.travelOrderMenu.forEach(item => {
						item.num = res.data[item.type];
					});
				});
				this.$api.getTeamOrderNum().then(res => {
					console.log(res, '获取订单数量');
					this.goodsOrderMenu.forEach(item => {
						item.num = res.data[item.type];
					});
					uni.stopPullDownRefresh()
				});
			}
		}
	};
</script>

<style lang="scss">
	.mine {
		background-color: #f9f9f9;
		padding: 10rpx 24rpx 40rpx;
		min-height: 100vh;
		box-sizing: border-box;

		.top_op_box {
			display: flex;
			align-items: center;
			justify-content: flex-end;
			margin-bottom: 10rpx;

			.op_item {
				text-align: center;
				margin-right: 46rpx;

				// &:last-child(1) {
				// 	margin-right: 0rpx;
				// }
				&:nth-of-type(4) {
					margin-right: 0rpx;
				}

				.op_item_name {
					font-size: 20rpx;
					font-weight: 400;
					color: #333333;
				}

				button {
					margin: 0;
					padding: 0;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					line-height: 24rpx;

					image {
						margin-bottom: 12rpx;
					}

					&::after {
						border: none;
						height: 0;
						width: 0;
					}

					.op_item_name {
						width: 50rpx;
					}
				}
			}
		}

		.user_box {
			display: flex;

			.user_img {
				margin-right: 15rpx;

				image {
					width: 96rpx;
					height: 96rpx;
					border-radius: 50%;
				}
			}

			.user_info {
				margin-bottom: 45rpx;

				.user_info_resources {
					display: flex;
					align-items: center;

					.resources_item {
						display: flex;
						align-items: center;
						background: #f0f0f0;
						border-radius: 21rpx;
						margin-right: 14rpx;
						padding: 6rpx 10rpx;
						font-size: 20rpx;
						font-weight: 400;
						color: #666666;

						&:nth-last-child(1) {
							margin-right: 0;
						}

						.resources_type_name {
							margin-right: 12rpx;
							flex: none;
						}

						.resources_num {
							margin-right: 10rpx;
						}

						image {
							flex: none;
						}
					}
				}
			}
		}

		.user_op_menu {
			display: grid;
			margin-bottom: 25rpx;
			grid-template-columns: repeat(3, 1fr);
			gap: 10rpx;

			.user_op_menu_item {
				width: 210rpx;
				height: 90rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background: #ffffff;
				border-radius: 20rpx;
				margin-right: 20rpx;
				margin-bottom: 20rpx;
			}
		}

		.area_order_box {
			padding: 24rpx;
			background: #ffffff;
			border-radius: 20rpx;
			margin-bottom: 20rpx;

			#gouper_menu {
				justify-content: flex-start;

				.order_menu_item {
					margin-right: 40rpx;
				}
			}

			.area_menu_list {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.order_menu_item {
					position: relative;

					image {
						display: block;
						margin: 0 auto;
					}

					.order_item_num {
						position: absolute;
						right: -8rpx;
						top: -14rpx;
						background-color: #f34035;
						border-radius: 50%;
						color: #fff;
						font-size: 16rpx;
						width: 30rpx;
						height: 30rpx;
						text-align: center;
						line-height: 30rpx;
						height: 30rpx;
					}
				}
			}

			&:nth-last-child(1) {
				.area_menu_list {
					justify-content: flex-start;

					.order_menu_item {
						margin-right: 40rpx;
					}
				}
			}
		}

		.docement_box {
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 40rpx;
		}

		.red_point {
			position: relative;

			&::after {
				content: "";
				position: absolute;
				top: -10rpx;
				right: -14rpx;
				width: 16rpx;
				height: 16rpx;
				background: #ff5634;
				border-radius: 50%;
			}
		}
	}
</style>